//
//  NoteDetailTableview.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/21.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "NoteDetailTableview.h"
#import "NoteDetailImageCel.h"
#import "NoteDetailCell.h"
#import "NoteCommentCell.h"
#import "ImageMode.h"
#import "NoteCommentMode.h"
#import "NoteCommentHeadCell.h"

static NSString *NoteDetailImageIdentifier = @"NoteDetailImageCel";
static NSString *NoteDetailIdentifier = @"NoteDetailCell";
static NSString *NoteCommentIdentifier = @"NoteCommentCell";
static NSString *NoteCommentHeadIdentifier = @"NoteCommentHeadCell";

@interface NoteDetailTableview () <UITableViewDelegate,UITableViewDataSource> {


}



@end

@implementation NoteDetailTableview


- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self defaultConfiguration];
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    [self defaultConfiguration];
}

- (void)defaultConfiguration {
    self.dataSource = self;
    self.delegate = self;
    [self registerNib:[UINib nibWithNibName:NoteDetailIdentifier bundle:nil] forCellReuseIdentifier:NoteDetailIdentifier];
    [self registerNib:[UINib nibWithNibName:NoteDetailImageIdentifier bundle:nil] forCellReuseIdentifier:NoteDetailImageIdentifier];
    [self registerNib:[UINib nibWithNibName:NoteCommentIdentifier bundle:nil] forCellReuseIdentifier:NoteCommentIdentifier];
    [self registerNib:[UINib nibWithNibName:NoteCommentHeadIdentifier bundle:nil] forCellReuseIdentifier:NoteCommentHeadIdentifier];

    self.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - UITableView Delegate & Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == NoteCellSection_title) {
        return 1;
    } else if (section == NoteCellSection_imgs) {
        return self.noteMode.noteImages.count;
    } else if (section == NoteCellSection_commentHead) {
        return 1;
    } else if (section == NoteCellSection_comment) {
        return self.noteMode.comments.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WeakSelf(ws);
    NSInteger rowIndex = indexPath.row;
    NSInteger sectionIndex = indexPath.section;
    if (sectionIndex == NoteCellSection_title) {
        NoteDetailCell  *cell = [tableView dequeueReusableCellWithIdentifier:NoteDetailIdentifier forIndexPath:indexPath];
            [cell cellWillDisplay:self.noteMode];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (sectionIndex == NoteCellSection_imgs) {
        ImageMode *imgMode = [self.noteMode.noteImages objectAtIndex:rowIndex];

        NoteDetailImageCel  *cell = [tableView dequeueReusableCellWithIdentifier:NoteDetailImageIdentifier forIndexPath:indexPath];
            [cell cellWillDisplay:imgMode];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (sectionIndex == NoteCellSection_commentHead) {
        
        NoteCommentHeadCell  *cell = [tableView dequeueReusableCellWithIdentifier:NoteCommentHeadIdentifier forIndexPath:indexPath];
        [cell cellWillDisplay:self.noteMode];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (sectionIndex == NoteCellSection_comment) {
        NoteCommentMode *comMode = [self.noteMode.comments objectAtIndex:rowIndex];

        NoteCommentCell  *cell = [tableView dequeueReusableCellWithIdentifier:NoteCommentIdentifier forIndexPath:indexPath];
        [cell cellWillDisplay:comMode];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger sectionIndex = indexPath.section;
    NSInteger rowIndex = indexPath.row;
    
    if (sectionIndex == NoteCellSection_title) {
       return [NoteDetailCell noteDetailCellHeight:self.noteMode];
    } else if (sectionIndex == NoteCellSection_imgs) {
        ImageMode *imgMode = [self.noteMode.noteImages objectAtIndex:rowIndex];
        return imgMode.scaleHeightToNotDetailWidth;
    }else if (sectionIndex == NoteCellSection_commentHead) {
        return 48;
    } else if (sectionIndex == NoteCellSection_comment) {
        NoteCommentMode *comMode = [self.noteMode.comments objectAtIndex:rowIndex];
        return [NoteCommentCell commentCellHeight:comMode];
    }
    return 0;
}


@end
